package cn.wolfcode.luowowo.controller;

import cn.wolfcode.luowowo.domain.Banner;
import cn.wolfcode.luowowo.domain.Travel;
import cn.wolfcode.luowowo.domain.UserInfo;
import cn.wolfcode.luowowo.interceptor.ResolverUserInfo;
import cn.wolfcode.luowowo.redis.AccessVo;
import cn.wolfcode.luowowo.redis.srevice.IUserRedisSrevice;
import cn.wolfcode.luowowo.service.*;
import cn.wolfcode.luowowo.service.impl.BannerServiceImpl;
import cn.wolfcode.luowowo.util.JsonResult;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;

@RestController
@RequestMapping("index")
public class IndexController {
    @Autowired
    private IBannerService bannerService;
    
    @Autowired
    private IAccessService accessService;
    
    @Autowired
    private IUserInfoService userInfoService;
    
    //前五个游记
    @GetMapping("/banners/travel")
    public Object bannersTravel() {
        //获得前五的是一个集合来装的
        List<Banner> travels = bannerService.queryTop5BySort();
        return JsonResult.success(travels);
    }
    //第一个攻略的接口
    @GetMapping("/banners/strategy")
    public Object bannersStrategy() {
        //第一个的攻略
        List<Banner> strategys = bannerService.queryStrategyTop5BySort();
        return JsonResult.success(strategys.get(0));
    }
    
    
    
    //访问综合数据
    @GetMapping("/banners/access")
    public Object access(String id, @ResolverUserInfo UserInfo userInfo) {
        //  userRedisSrevice.setAccessUser(userInfo,user);
        //添加访问用户
        //当前登录用户不是作者 就进行访问登记
        if (!id.equals(userInfo.getId())) {
            accessService.setAccess(userInfo.getId(), id);
        }
        
        
        //返回前8的访问用户
        List<UserInfo> list = accessService.getAccess(id);
        
        
        //返回总访问次数
        int accessNum = accessService.getAccessNum(id);
        
        //返回今天的访问数
        Long accessday = accessService.getAccessNumDay(id);
        
        HashMap<String, Object> map = new HashMap<>();
        Collections.reverse(list);
        map.put("user", list);
        map.put("totalView", accessNum);
        map.put("todayView", accessday);
        return JsonResult.success(map);
    }
    

    
    //获取访问人数总数
    @GetMapping("/banners/accessnum")
    public Object accessnum(String user) {
        
        List<UserInfo> list = accessService.getAccess(user);
        //获取所有访问的用户
        return JsonResult.success(list);
    }
    
    //获取所有的访问人员
    @GetMapping("/banners/accessSum")
    public Object accessSum(String user) {
        List<UserInfo> list = accessService.getAccessSum(user);
        //获取所有访问的用户
        return JsonResult.success(list);
    }
}
